/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.action.crud;

import e.z.blog.back.action.AbstractAction;
import e.z.blog.back.action.CrudBaseApi;
import e.z.blog.back.com.ComDataTableApi;
import e.z.blog.back.com.ComFixNutzStringBugApi;
import e.z.blog.back.com.DaoLanApi;
import e.z.blog.back.com.DaoTabApi;
import e.z.blog.back.bean.base.Tab;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import z.h.w.jar.kit.To;

/**
 *
 * @author EnzoZhong
 */
@IocBean
@At( "/tab" )
public class CrudTab extends AbstractAction implements CrudBaseApi<Tab> {

        final private String type = "tab";
        final private String[] column = To.objs2Array( "tabId" , "tabName" , "state" );
        @Inject
        private ComFixNutzStringBugApi comFixNutzStringBug;
        @Inject
        private ComDataTableApi comDataTable;
        @Inject
        private DaoTabApi daoTab;
        @Inject
        private DaoLanApi daoLan;

        @Override
        @At( "" )
        @Ok( "jsp:back.crudTab" )
        public Map<String , Object> page ( HttpServletRequest request ) {
                Map<String , Object> map = this.createMap( request , type );
                map.put( "aoColumns" , comDataTable.getTitleList( Tab.class , column ) );
                map.put( "lanList" , daoLan.readAll() );
                map.put( "tabList" , daoTab.readAll() );
                return map;
        }

        @Ok( "json" )
        @At( CREATE )
        @AdaptBy( type = JsonAdaptor.class )
        public Tab create ( @Param( type ) Tab tab , @Param( "translate" ) Map<String , String> tmp ) {
                Map<String , String> translate = comFixNutzStringBug.fixString( tmp );
                tab.setTranslate( translate );
                return daoTab.create( tab );
        }

        @Override
        @Ok( "json" )
        @At( READ_BY_ID )
        @AdaptBy( type = JsonAdaptor.class )
        public Tab read ( @Param( type ) Tab tab ) {
                return daoTab.read( tab );
        }

        @Ok( "json" )
        @At( READ_ALL )
        @AdaptBy( type = JsonAdaptor.class )
        public List<List<String>> readAll () {
                List<List<String>> stringList = new LinkedList();
                List<Tab> cityList = daoTab.readAll();
                for ( Tab tab : cityList ) {
                        stringList.add( comDataTable.getValueList( tab , column ) );
                }
                return stringList;
        }

        @Ok( "json" )
        @At( UPDATE )
        @AdaptBy( type = JsonAdaptor.class )
        public Tab update ( @Param( type ) Tab tab , @Param( "translate" ) Map<String , String> tmp ) {
                Map<String , String> translate = comFixNutzStringBug.fixString( tmp );
                tab.setTranslate( translate );
                return daoTab.update( tab );
        }

        @Override
        @Ok( "json" )
        @At( DELETE )
        @AdaptBy( type = JsonAdaptor.class )
        public Tab delete ( @Param( type ) Tab tab ) {
                return daoTab.delete( tab );
        }

        @Override
        public Tab create ( Tab type ) {
                throw new UnsupportedOperationException( "Not supported yet." );
        }

        @Override
        public Tab update ( Tab type ) {
                throw new UnsupportedOperationException( "Not supported yet." );
        }
}
